package calendar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author angelika
 */
public class Controls extends JPanel {
    Integer year;
    Integer month;
    Calendar parent;
    
    JLabel yearLabel;
    
    public Controls(Calendar cal) {
        parent = cal;
        year = GregorianCalendar.getInstance().get(GregorianCalendar.YEAR);
        month = GregorianCalendar.getInstance().get(GregorianCalendar.MONTH);
        
        setOpaque(false);
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        
        String[] months = {"Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", 
        "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь"};
        
        JComboBox box = new JComboBox(months);
        JButton previousYear = new JButton("<");
        yearLabel = new JLabel(year.toString());
        JButton nextYear = new JButton(">");
        
        box.setOpaque(false);
        box.setSelectedIndex(month);
        
        previousYear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                year = year - 1;
                yearLabel.setText(year.toString());
                parent.newDate(year, month);
            }
        });
        
        nextYear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                year = year + 1;
                yearLabel.setText(year.toString());
                parent.newDate(year, month);
            }
        });
        
        box.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                month = box.getSelectedIndex();
                parent.newDate(year, month);
            }
        });
        
        box.setBorder(BorderFactory.createEmptyBorder(20,10,20,10));
        previousYear.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        yearLabel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        nextYear.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        
        add(box);
        add(previousYear);
        add(yearLabel);
        add(nextYear);
    }
}
